//
//  BVMyOrderViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/4.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVMyOrderViewController.h"
#import "BVMyOrderCell.h"
#import "BVMyOrderDetailsViewController.h"
#import "BVLoginoutMyOrder.h"
#import "BVLoginViewController.h"
#import "BVEmptyMyOrder.h"
#define kBVMyOrderCell @"BVMyOrderCell"

@interface BVMyOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) BVLoginoutMyOrder *loginoutOrder;
@property (nonatomic,strong) BVEmptyMyOrder *emptyMyOrder;
@end

@implementation BVMyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的订单";
    
    [self chooseChildView];
    
    //添加我的订单列表
    //[self setUI];
}
-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 120;
        [self.tableView registerClass:[BVMyOrderCell class] forCellReuseIdentifier:kBVMyOrderCell];
    }
    return _tableView;
}
-(void)chooseChildView{
    
    if (! self.user.login) {
        self.loginoutOrder = [[BVLoginoutMyOrder alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
        [self.view addSubview:self.loginoutOrder];
        __weak __typeof(self) weakSelf = self;
        self.loginoutOrder.logoutBlock = ^(){
            BVLoginViewController * login = [[BVLoginViewController alloc]init];
            [weakSelf.navigationController pushViewController:login animated:YES];
        };
        
    }else{
        [self.loginoutOrder removeFromSuperview];
        
        if (self.viewType == 0) {
            self.emptyMyOrder = [[BVEmptyMyOrder alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
            [self.view addSubview:self.emptyMyOrder];
            __weak __typeof(self) weakSelf = self;
            self.emptyMyOrder.goShopBlock = ^() {
                 weakSelf.tabBarController.selectedIndex = 1;
            };
        }else{
            [self.emptyMyOrder removeFromSuperview];
        }
    }
    
}

-(void)setUI{
    
    [self.view addSubview:self.tableView];
       
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return  section == 0 ? 10:5;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return section == 4 ? 5:5;
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc]initWithFrame:self.view.bounds];
    return view;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc]initWithFrame:self.view.bounds];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BVMyOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:kBVMyOrderCell forIndexPath:indexPath];
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    BVMyOrderDetailsViewController * orderDetails = [[BVMyOrderDetailsViewController alloc]init];
    [self.navigationController pushViewController:orderDetails animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
